package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.entity.Comments;
import com.itheima.entity.Preject;
import com.itheima.mapper.CommentsMapper;
import com.itheima.pojo.po.CommentsPo;
import com.itheima.pojo.po.UserPo;
import com.itheima.result.PageResult;
import com.itheima.service.CommentsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author guanhuaer
 * @since 2024-09-15
 */
@Service
public class CommentsServiceImpl extends ServiceImpl<CommentsMapper, Comments> implements CommentsService {
    @Autowired
    private CommentsMapper commentsMapper;

    /**
     * 添加评论
     *
     * @param comments
     */
    @Override
    public void contentsAdd(Comments comments) {
        Date dNow = new Date();
        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        comments.setSupport(0);
        comments.setIsLiked(0);
        comments.setCreateTime(ft.format(dNow));
        commentsMapper.commentsAdd(comments);
    }

    /**
     * 评论点赞
     *
     * @param comments
     */
    @Override
    public void commentSupport(Comments comments) {
        Integer support = commentsMapper.selectSupport(comments.getId());
        if (comments.getSupport() == 1) {
            support += 1;
        } else {
            support -= 1;
        }
        commentsMapper.updateSupport(comments.getId(), support);

    }

    /**
     * 根据项目id查询评论列表
     *
     * @param id
     * @return
     */
    @Override
    public List<CommentsPo> selectByProjectId(Integer id) {
        //获取被评论用户的用户名和头像
        List<CommentsPo> comments = commentsMapper.selectProjectId(id);
        if (comments == null) {
            return null;
        }
        CommentsPo po = new CommentsPo();
        for (CommentsPo commentsPo : comments) {
            //获取评论子评论
            List<CommentsPo> subComments = commentsMapper.selectSubComments(commentsPo.getId());
            if (subComments != null) {
                for (CommentsPo subComment : subComments) {
                    po = commentsMapper.selectProjectIds(subComment.getId());
                    subComment.setBusername(po.getBusername());
                    subComment.setBuserImg(po.getBuserImg());
                }
            }
            commentsPo.setChildren(subComments);
        }
        return comments;
    }

    @Override
    public Comments contentsFindNew(Comments comments) {
        Comments comments1 = commentsMapper.contentsFindNew(comments);
        CommentsPo comments2 = commentsMapper.selectProjectIds(comments1.getId());
        comments1.setBusername(comments2.getBusername());
        comments1.setBuserImg(comments2.getBuserImg());
        return comments1;
    }

    @Override
    public PageResult selectByUserId(CommentsPo comment) {
        PageHelper.startPage(comment.getPageNum(), comment.getPageSize());
        Page<CommentsPo> commentsPos = commentsMapper.selectCommentsList(comment);
        CommentsPo po = new CommentsPo();
        for (CommentsPo commentsPo : commentsPos) {
            po = commentsMapper.selectProjectIds(commentsPo.getId());
            commentsPo.setBusername(po.getBusername());
            commentsPo.setBuserImg(po.getBuserImg());
        }
        return new PageResult(commentsPos.getTotal(), commentsPos.getResult());
    }

    /**
     * 评论删除
     * @param ids
     */
    @Override
    public void commentDelete(Integer[] ids) {
        commentsMapper.commentDelete(ids);
    }

    @Override
    public List<UserPo> getMemberInfo(List<String> usernames) {
        List<UserPo> pos= new ArrayList<>();
        for (String username : usernames) {
            pos.add(commentsMapper.getMemberInfo(username));
        }
        return pos;
    }

    public List<String> contentsFindAll() {
        return commentsMapper.contentsFindAll();
    }
}
